﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CognisessDA;
using CognisessService;
using CognisessWeb.Models;

namespace CognisessWeb.Controllers
{
    public class NewsController : Controller
    {
        //
        // GET: /News/
        AccountBLL accountBLL = new AccountBLL();

        public ActionResult Create()
        {
            return View();
        }

        public ActionResult Edit(int id)
        {
            List<NewsInfo> news = accountBLL.GetNews(new NewsInfo { Id = id });
            List<Post> posts = new List<Post>();
            NewsModel bindNews = new NewsModel();
            foreach (var item in news)
            {
                bindNews.Id = item.Id;
                bindNews.NewsName = item.NewsName;
                bindNews.Title = item.Title;
                bindNews.NewsContent = item.NewsContent;
                bindNews.CreateTime = DateTime.Parse(item.CreateTime.ToString());
                bindNews.Posts = new List<PostsModel> { };
                posts = accountBLL.GetPost(new Post { NewsId = item.Id });
                for (int i = 0; i < posts.Count; i++)
                {
                    bindNews.Posts.Add(new PostsModel { NewsId = bindNews.Id, Title = bindNews.Title, Content = posts[i].PostContent, Id = posts[i].Id, DateCreated = DateTime.Parse(posts[i].dateCreated.ToString()) });
                }
            }
            return View(bindNews);
        }

        //  POST: /Account/CreateNews
        [HttpPost]
        public ActionResult Create(NewsInfo newsInfo)
        {
            List<NewsInfo> newsInfos = new List<NewsInfo>();
            newsInfo.CreateTime = DateTime.Now;
            newsInfos.Add(newsInfo);
            accountBLL.AddNews(newsInfos);
            return RedirectToAction("Index");
        }

        //  POST: /Account/EditNews/Id 
        [HttpPost]
        public ActionResult Edit(NewsInfo newsInfo)
        {
            List<NewsModel> newsModel = new List<NewsModel>();
            List<NewsInfo> newInfos = new List<NewsInfo>();
            newsInfo.CreateTime = DateTime.Now;
            newInfos.Add(newsInfo);
            accountBLL.UpdataNews(newInfos);
            return RedirectToAction("Index");
        }


        public ActionResult Delete(int id)
        {

            bool isSuccessful = accountBLL.DeleteNews(id);
            if (isSuccessful)
            {
                TempData["ExcuteSuccfull"] = "Delete Successfully.";
            }
            else
            {
                TempData["ExcuteSuccfull"] = "Operation fail. Please contact with the administrator.";
            }
            return RedirectToAction("Index");
        }

        

        public ActionResult Index()
        {
            List<NewsModel> listNews = new List<NewsModel>();
            List<NewsInfo> news = accountBLL.GetNews(new NewsInfo());
            List<Post> posts = new List<Post>();
            foreach (var item in news)
            {
                NewsModel bindNews = new NewsModel();
                bindNews.Id = item.Id;
                bindNews.NewsName = item.NewsName;
                bindNews.Title = item.Title;
                bindNews.NewsContent = item.NewsContent;
                bindNews.CreateTime = DateTime.Parse(item.CreateTime.ToString());
                bindNews.Posts = new List<PostsModel> { };
                posts = accountBLL.GetPost(new Post { NewsId = bindNews.Id });
                for (int i = 0; i < posts.Count; i++)
                {
                    bindNews.Posts.Add(new PostsModel { NewsId = bindNews.Id, Title = bindNews.Title, Content = posts[i].PostContent, Id = posts[i].Id, DateCreated = DateTime.Parse(posts[i].dateCreated.ToString()) });
                }
                listNews.Add(bindNews);
            }
            //if (page == null)
            //    page = 0;
            //const int pageSize = 3;
            //var list = listNews.Skip((page ?? 0) * pageSize).Take(pageSize);
            return View(listNews);
        }

        // GET: /UserMaster/NewsDetails/Id
        public ActionResult Details(int Id)
        {
            List<NewsInfo> news = accountBLL.GetNews(new NewsInfo { Id = Id });
            List<Post> posts = new List<Post>();
            NewsModel bindNews = new NewsModel();
            foreach (var item in news)
            {
                bindNews.Id = item.Id;
                bindNews.NewsName = item.NewsName;
                bindNews.Title = item.Title;
                bindNews.NewsContent = item.NewsContent;
                bindNews.CreateTime = DateTime.Parse(item.CreateTime.ToString());
                bindNews.Posts = new List<PostsModel> { };
                posts = accountBLL.GetPost(new Post { NewsId = item.Id });
                for (int i = 0; i < posts.Count; i++)
                {
                    bindNews.Posts.Add(new PostsModel { NewsId = bindNews.Id, Title = bindNews.Title, Content = posts[i].PostContent, Id = posts[i].Id, DateCreated = DateTime.Parse(posts[i].dateCreated.ToString()) });
                }
            }
            return View(bindNews);
        }

    }
}
